package com.ecoolex.hotel.saas.platform.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.google.common.collect.Lists;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.StoreAd;
import com.ecoolex.hotel.saas.entity.StoreAdPosition;
import com.ecoolex.hotel.saas.enums.StoreAdStatus;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.AddStoreAdDTO;
import com.ecoolex.hotel.saas.pojo.EditStoreAdDTO;
import com.ecoolex.hotel.saas.pojo.EditStoreAdPositionDTO;
import com.ecoolex.hotel.saas.pojo.QueryStoreAdDTO;
import com.ecoolex.hotel.saas.ws.StoreAdService;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;


@GraphQL2Module(type = Type.Platform, description = "商户广告管理")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
key = StoreAdGraphql.AUTH,
name = "商户广告管理",
remark = "商户广告管理")
public class StoreAdGraphql {

    public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-STORE-AD";

    @Reference
    private StoreAdService service;

    @Autowired
    private StorageService storageService;

    @TypeField(type = StoreAd.class, field = "image", description = "图片")
    public StorageInfo image(@Source StoreAd source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = StoreAd.class, field = "positionList", description = "广告推送位置")
    public List<StoreAdPosition> positionList(@Source StoreAd source) {
        return service.queryAdPositionByAdId(source.getAdId());
    }

    @TypeField(type = StoreAd.class, field = "canDelete", description = "是否能删除")
    public boolean canDelete(@Source StoreAd source) {
        return StoreAdStatus.canDelete(source);
    }

    @TypeField(type = StoreAd.class, field = "canRelease", description = "是否能发布")
    public boolean canRelease(@Source StoreAd source) {
        return StoreAdStatus.canRelease(source);
    }

    @TypeField(type = StoreAd.class, field = "canOffline", description = "是否能下线")
    public boolean canOffline(@Source StoreAd source) {
        return StoreAdStatus.canOffline(source);
    }

    private static final String AUTH_GET = AUTH + "-GET";

    @QueryField(field = "storeAd", description = "获取广告详情")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "添加部门")
    public StoreAd ad(@NonNull Integer adId, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return service.getAdByAdId(adId);
    }

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    @QueryField(field = "storeAdList", description = "查询广告列表")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询广告列表")
    public Pagination<StoreAd> queryAd(@NonNull QueryStoreAdDTO query, Sorting[] sorts, @NonNull Page page, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return service.queryAd(query, sorts, page);
    }

    @MutationField(field = "uploadStoreAdImage", description = { "上传广告图片" })
    public StorageInfo uploadGoodsBrandPic(@NonNull Upload request, PlatformMemberAuthority user) {
        if (!user.auth(AUTH_ADD) && !user.auth(AUTH_UPDATE)) {
            user.notAuthority();
        }

        UploadSource source = new UploadSource("StoreAd", "Image");

        UploadLimit limit = new UploadLimit(FileSize.MB.byteLength(10), MimeType.IMAGE.commons());

        return storageService.upload(request, source, limit);
    }

    private static final String AUTH_ADD = AUTH + "-ADD";

    @MutationField(field = "addStoreAd", description = "添加广告")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加广告")
    public StoreAd add(@NonNull AddStoreAdDTO add, @NonNull List<EditStoreAdPositionDTO> positionList, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return service.addAd(add, Lists.newArrayList(positionList), user.userName());
    }

    private static final String AUTH_UPDATE = AUTH + "-UPDATE";

    @MutationField(field = "editStoreAd", description = "修改广告")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_UPDATE, name = "修改广告")
    public StoreAd edit(@NonNull EditStoreAdDTO edit, @NonNull List<EditStoreAdPositionDTO> positionList, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_UPDATE);
        return service.updateAd(edit, Lists.newArrayList(positionList), user.userName());
    }

    private static final String AUTH_RELEASE = AUTH + "-RELEASE";

    @MutationField(field = "releaseStoreAd", description = "发布广告")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_RELEASE, name = "发布广告")
    public boolean release(int adId, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_RELEASE);
        service.releaseAd(adId, user.userName());
        return true;
    }

    private static final String AUTH_OFFLIEN = AUTH + "-OFFLIEN";

    @MutationField(field = "offlienStoreAd", description = "下线广告")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLIEN, name = "下线广告")
    public boolean offlien(int adId, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_OFFLIEN);
        service.offlineAd(adId, user.userName());
        return true;
    }

    private static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delStoreAd", description = "删除广告")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除广告")
    public boolean delete(int adId, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        service.deleteAd(adId, user.userName());
        return true;
    }

}
